package com.csmzxy.soft.atm;

import java.util.ArrayList;
import java.util.List;

import com.csmzxy.soft.comm.Account;
import com.csmzxy.soft.comm.Customer;
import com.csmzxy.soft.comm.Status;

public class ATMLocalService implements IATMService {
	private List<Customer> listCustomer;
	private List<Account> listAccount;
	public ATMLocalService(){
		listCustomer=new ArrayList<Customer>();
		Customer customer=new Customer("张三","43045451545454","1395554545");
		listCustomer.add(customer);
		listAccount =new ArrayList<Account>();
		Account acc1=new Account("123456","123456",10000,customer);
		Account acc2=new Account("888888","888888",30000,customer);
		listAccount.add(acc1);
		listAccount.add(acc2);
	}
	public Status login(String accId,String pwd){
		Account acc=findAccount(accId);
		if(acc==null)
			return Status.INVALID_ID;
		if(acc.getPassword().compareTo(pwd)==0)
			return Status.OK;
		return Status.INVALID_PWD;
	}
	public double getBalance(String accId){
		Account acc=findAccount(accId);
		if(acc==null){
			return -1;
		}
		return acc.getBalance();
	}
	public Status withDraw(String accId,double money){
		Account acc=findAccount(accId);
		if(acc==null){
			return Status.INVALID_ID;
		}
		if(acc.withdraw(money))
			return Status.OK;
		return Status.SHORTGE;
	}
	public Status deposit(String accId,double money){
		Account acc=findAccount(accId);
		if(acc==null)
			return Status.INVALID_ID;
		acc.deposit(money);
		return Status.OK;
	}
	public String getTransactions(String accId){
		Account acc=findAccount(accId);
		if(acc==null)
			return "无效的账号";
		return acc.getTransRecord();
	}
	private Account findAccount(String accId) {
		for(Account acc:listAccount){
			if(acc.getId().compareTo(accId)==0){
				return acc;
			}
		}
		return null;
	}
	public Status changePwd(String accId,String oldPwd,String newPwd){
		Account acc = findAccount(accId);
		if(acc==null){
			return Status.INVALID_ID;
		}
		if(login(accId, oldPwd)==Status.OK){
			acc.setPassword(newPwd);
			return Status.OK;
		}
		
		return Status.ERROR;
	}
	public Status transfer(String fromAccId,String toAccId,double money){
		Account acc=findAccount(fromAccId);
		if(acc==null){
			return Status.INVALID_ID;
		}
		if(acc.transfer(acc, money)){
			return Status.OK;
		}
		return Status.ERROR;
	}
}
